package com.eastcom.collection.provider

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.util.CollectionUtils;
import com.eastcom.collection.context.*
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

public class PropertyPlaceholderConfigurerProvider extends PropertyPlaceholderConfigurer {
	private final static Log log = LogFactory.getLog(PropertyPlaceholderConfigurerProvider.class);
    private static List<String> propFileList
	private static Map<String, Object> ctxPropertiesMap;
	
	public PropertyPlaceholderConfigurerProvider() {
		super();
		// TODO 自动生成的构造函数存根
        List<String> propList = ServerContext.propertyFilePath
        propFileList = propList
        setGollfPropFiles(propList);
    }

    public static void loadProperties(){
        ctxPropertiesMap = new HashMap<>()
        for (String gollfPropFile : propFileList) {
            try {
                Properties prop = new Properties();
                prop.load(new FileInputStream(new File(gollfPropFile)));
                if(prop != null) {
                    for (Object key : prop.keySet()) {
                        String keyStr = key.toString();
                        String value = prop.getProperty(keyStr);
                        ctxPropertiesMap.put(keyStr, value);
                    }
                }
            } catch (Exception e) {
                System.out.println("配置文件内容获取失败！")
            }
        }
    }

	@Override
	protected void processProperties(ConfigurableListableBeanFactory beanFactory,
			Properties props) throws BeansException {
		super.processProperties(beanFactory, props);
		// load properties to ctxPropertiesMap
		ctxPropertiesMap = new HashMap<String, Object>();
		for (Object key : props.keySet()) {
			String keyStr = key.toString();
			String value = props.getProperty(keyStr);
			ctxPropertiesMap.put(keyStr, value);
		}
		log.error("初始化加载GollfPropertyPlaceholderConfigurer文件成功");
	}    
	//static method for accessing context propertiespublic 
	public static Object getContextProperty(String name){
		return ctxPropertiesMap.get(name);
	}    
	public void setGollfPropFiles(List<String> gollfPropFiles) {
		Properties properties = new Properties();
		for (String gollfPropFile : gollfPropFiles) {
			try {
				log.info("Loading properites file from " + gollfPropFile);
				Properties prop = new Properties();
				prop.load(new FileInputStream(new File(gollfPropFile)));
				log.debug("Properties -> " + prop);
				if(prop != null) {
					properties.putAll(prop);
				}
			} catch (Exception e) {
				log.error("Properties file cannot be found. All related functionalities may be unavailable");
			}
		}
		this.setProperties(properties); //关键方法,调用的PropertyPlaceholderConfigurer中的方法,
		//通过这个方法将自定义加载的properties文件加入spring中
	}

	@Override
	protected void loadProperties(Properties props) throws IOException {
		// TODO 自动生成的方法存根
		super.loadProperties(props);
	}
	
	@Override
	protected Properties mergeProperties() throws IOException {
		Properties result = new Properties();

		if (this.localOverride) {
			// Load properties from file upfront, to let local properties override.
			loadProperties(result);
		}

		if (this.localProperties != null) {
			for (Properties localProp : this.localProperties) {
				//将用户自定义加载的属性值,与spring加载的合并
				CollectionUtils.mergePropertiesIntoMap(localProp, result); 
			}
		}

		if (!this.localOverride) {
			// Load properties from file afterwards, to let those properties override.
			loadProperties(result);
		}

		return result;
	}
}
